public class AddBinary {
    public String addBinary(String a, String b) {
        int n1 = a.length(), n2 = b.length();
        int cur1 = n1 - 1, cur2 = n2 - 1;
        int t = 0;
        StringBuilder str = new StringBuilder();
        while(cur1 >= 0 || cur2 >= 0 || t != 0) {
            if(cur1 >= 0) {
                t += a.charAt(cur1) - '0';
                cur1--;
            }
            if(cur2 >= 0) {
                t += b.charAt(cur2) - '0';
                cur2--;
            }

            str.append((char)((t % 2) + '0'));
            t /= 2;
        }
        str.reverse();
        return str.toString();
    }
}
